from ffmpy import FFmpeg
from pathlib import Path
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def new_folder(tf):
    output_folder = Path(tf)
    if output_folder.exists():  # 判断路径是否存在
        logger.info("--- 已经有这个文件夹! ---")
    else:
        output_folder.mkdir(parents=True)
        logger.info(f"--- 创建了新文件夹!{output_folder} ---")
    return output_folder
def video_coding(fp,tf):
    '''硬件解码+硬件编码cpu使用率很低速率8.3x h264CPU编码速率1.7x'''
    path = Path(fp)
    if tf is None or tf == "":
        to = 'TO'
        file_pt = path.parent  # 获取文件的父级文件夹路径
    else:
        to = ''
        file_pt = new_folder(tf)  # 创建或验证输出文件夹
    # 检查输入文件是否存在
    if not path.exists():
        logger.error(f"--- 输入文件不存在: {path} ---")
        return
    ff = FFmpeg(
        inputs={fp: '-hwaccel videotoolbox'},
        # outputs={f'{sf}/{filename_suffix}.mp4':'-c:v h264_videotoolbox -b:v 1M -c:a copy -y'}
        # inputs={vp:'-hwaccel videotoolbox -c:v hevc_videotoolbox'},
        outputs={f'{file_pt}/{path.stem}{to}.mp4':'-c:v h264_videotoolbox -b:v 2000K -c:a copy -y'}
    )
    ff.run()
if __name__ == '__main__':
    filepath = r"/Volumes/ST/Documents/videos/4K.DEMO/4K50FPS 我爱你中国.mp4"
    target_folder = r""
    video_coding(filepath,target_folder)
